package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.qbw.actionmanager.ActionManager;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.action.ElectricAreaWarningModeSelectedAction;
import com.qbw.app.databinding.VehicleActivityElectricAreaWarningModeSelectBinding;
import com.qbw.app.model.ElectricArea;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.vehicle.adapter.VehicleAdapter;
import com.qbw.app.module.vehicle.holder.VehicleElectricAreaModeItemHolder;

/**
 * @author QBW
 * @date 2019/10/8
 */
public class VehicleElectricAreaWarningModeSelectActivity extends CommonActivity<VehicleActivityElectricAreaWarningModeSelectBinding> implements VehicleElectricAreaModeItemHolder.Listener {

    private VehicleAdapter mAdapter;
    private ElectricArea.Mode.ModeList mModeList;

    public static void start(Context context, ElectricArea.Mode.ModeList modeList) {
        Intent intent = new Intent(context, VehicleElectricAreaWarningModeSelectActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(Constant.KEY_WARNING_MODE_LIST, modeList);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_electric_area_warning_mode_select;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mModeList = (ElectricArea.Mode.ModeList) args.getSerializable(Constant.KEY_WARNING_MODE_LIST);
        mBinding.title.setListener(this);
        mBinding.rcv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new VehicleAdapter(this);
        mBinding.rcv.setAdapter(mAdapter);
        mAdapter.addChild(mModeList.getList());
    }

    @Override
    public void onLeftAreaClick() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        ActionManager.getInstance()
                     .triggerAction(new ElectricAreaWarningModeSelectedAction(mModeList));
        super.onBackPressed();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(Constant.KEY_WARNING_MODE_LIST, mModeList);
    }

    @Override
    public int onGetElectricWarningModeSelectedCount() {
        return mModeList.getSelectedModeList().size();
    }
}
